package com.example.houseman.service.impl;

import com.example.houseman.entity.TbHouseStyle;
import com.example.houseman.entity.TbRelationRoomPicture;
import com.example.houseman.entity.TbRoomInfo;
import com.example.houseman.mapper.TbHouseStyleMapper;
import com.example.houseman.mapper.TbRelationRoomPictureMapper;
import com.example.houseman.service.ITbHouseStyleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.org.dto.TbHouseStyleDto;
import com.org.dto.TbRoomInfoDto;
import com.org.vo.ResultEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 集中式房源-房型信息表 服务实现类
 * </p>
 *
 * @author 龚梦
 * @since 2021-04-01
 */
@Service
@Slf4j
public class TbHouseStyleServiceImpl extends ServiceImpl<TbHouseStyleMapper, TbHouseStyle> implements ITbHouseStyleService {

    @Autowired
    private TbHouseStyleMapper tbHouseStyleMapper;


    @Autowired
    private TbRelationRoomPictureMapper relationRoomPictureMapper;

    @Override
    @Transactional
    public ResultEntity savehsl(TbHouseStyleDto tbHouseStyleDto) {
        String pids = "";
        TbHouseStyle tbHouseStyle = new TbHouseStyle();
        BeanUtils.copyProperties(tbHouseStyleDto, tbHouseStyle);
        Integer[] duiliids = tbHouseStyleDto.getDuiliids();
        for (int i = 0; i < duiliids.length; i++) {
            pids += duiliids[i];
            if (i < duiliids.length) {
                pids += ",";
            }
        }
        tbHouseStyle.setPrivateFacility(pids);
        //tbHouseStyle.setHouseId();
        log.info(tbHouseStyle.toString() + "====独立=====");
        int insert = tbHouseStyleMapper.insert(tbHouseStyle);
        String[] imageUrl = tbHouseStyleDto.getImageUrl();
        for (String s : imageUrl) {
            TbRelationRoomPicture tbRelationRoomPicture=new TbRelationRoomPicture();
            tbRelationRoomPicture.setRoomId(tbHouseStyle.getId());
            tbRelationRoomPicture.setPictureUrl(s);
            log.info(tbRelationRoomPicture.toString()+"===");
            relationRoomPictureMapper.insert(tbRelationRoomPicture);
        }


        if (insert > 0) {
            return ResultEntity.ok("增加成功");
        } else {
            return ResultEntity.error("增加失败");
        }
    }
}
